package com.cy.aircompressor.main.ui.lifecycle.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;

import com.cy.aircompressor.api.RepositoryImpl;
import com.cy.aircompressor.entity.component.Component;
import com.cy.aircompressor.entity.compressor.Compressor;
import com.cy.aircompressor.entity.maintenance.MaintenanceRecord;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.drug.lib.util.DLog;
import com.drug.lib.view.recycptr.bean.RowEntity;
import com.net.model.base.Resource;
import com.net.model.viewmodel.BaseViewModel;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2021/11/12 or 23:27
 * 描述：---
 */
public class LifeCycleViewModel extends BaseViewModel<RepositoryImpl> {
    private MediatorLiveData<Resource<RowEntity<MaintenanceRecord>>> queryMaintenanceRecordByPageObservable;
    private MediatorLiveData<Resource<RowEntity<Compressor>>> compressorObservable;
    private MediatorLiveData<Resource<RowEntity<Sensor>>> querySensorByPageObservable;
    private MediatorLiveData<Resource<RowEntity<Component>>> queryComponentByPageObservable;
    private MediatorLiveData<Resource<MaintenanceRecord>> queryMaintenanceRecordById;

    public LifeCycleViewModel(@NonNull Application application) {
        super(application);
        bindBaseModel(RepositoryImpl.get());
        queryMaintenanceRecordByPageObservable = new MediatorLiveData<>();
        compressorObservable = new MediatorLiveData<>();
        querySensorByPageObservable = new MediatorLiveData<>();
        queryComponentByPageObservable = new MediatorLiveData<>();
        queryMaintenanceRecordById = new MediatorLiveData<>();
    }

    public void queryMaintenanceRecordByPage(Integer pageNum, Integer pageSize, String compressorId, String powerStationId) {
        queryMaintenanceRecordByPageObservable.addSource(baseModel.queryMaintenanceRecordByPage(pageNum, pageSize, compressorId, powerStationId), new Observer<Resource<RowEntity<MaintenanceRecord>>>() {
            @Override
            public void onChanged(Resource<RowEntity<MaintenanceRecord>> rowEntityResource) {
                queryMaintenanceRecordByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public void queryMaintenanceRecordById(String id) {
        queryMaintenanceRecordById.addSource(baseModel.queryMaintenanceRecordById(id), new Observer<Resource<MaintenanceRecord>>() {
            @Override
            public void onChanged(Resource<MaintenanceRecord> rowEntityResource) {
                queryMaintenanceRecordById.setValue(rowEntityResource);
            }
        });
    }

    public void compressor(Integer pageNum, Integer pageSize, String powerStationId, String sn) {
        compressorObservable.addSource(baseModel.compressor(pageNum, pageSize, powerStationId, sn), new Observer<Resource<RowEntity<Compressor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Compressor>> authBeanResource) {
                compressorObservable.setValue(authBeanResource);
            }
        });
    }

    public void querySensorByPage(Integer pageNum, Integer pageSize, String compressorId, String powerStationId, String sn, Integer type) {
        querySensorByPageObservable.addSource(baseModel.querySensorByPage(pageNum, pageSize, compressorId, powerStationId, null, sn, type), new Observer<Resource<RowEntity<Sensor>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Sensor>> rowEntityResource) {
                querySensorByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public void queryComponentByPage(Integer pageNum, Integer pageSize, String compressorId, String powerStationId, String compressorPartCode, String sn) {
        queryComponentByPageObservable.addSource(baseModel.queryComponentByPage(pageNum, pageSize, compressorId, powerStationId, compressorPartCode, sn), new Observer<Resource<RowEntity<Component>>>() {
            @Override
            public void onChanged(Resource<RowEntity<Component>> rowEntityResource) {
                queryComponentByPageObservable.setValue(rowEntityResource);
            }
        });
    }

    public MediatorLiveData<Resource<RowEntity<MaintenanceRecord>>> getQueryMaintenanceRecordByPageObservable() {
        return queryMaintenanceRecordByPageObservable;
    }

    public MediatorLiveData<Resource<RowEntity<Compressor>>> getCompressorObservable() {
        return compressorObservable;
    }

    public MediatorLiveData<Resource<RowEntity<Sensor>>> getQuerySensorByPageObservable() {
        return querySensorByPageObservable;
    }

    public MediatorLiveData<Resource<RowEntity<Component>>> getQueryComponentByPageObservable() {
        return queryComponentByPageObservable;
    }

    public MediatorLiveData<Resource<MaintenanceRecord>> getQueryMaintenanceRecordById() {
        return queryMaintenanceRecordById;
    }
}
